/**
\file unicode_to_big5.c

COPYRIGHT(C) 2011,ShenZhen smit CO.,LTD. \n
All rights reserved. \n\n

\brief implement the charset interface.

\details charset interface includes:

\version 1.0
\date 2012-6-8 Wednesday
\author wangxr <yangfeng@aaa.com>
\note Created

*/

/* Includes --------------------------------------------------------------- */
#include <stdio.h>
#include <stddef.h>
#include <string.h>
#include "usm_typedef.h"

#include "usm_charset.h"
#include "unicode_to_big5.h"

/* Private Macros --------------------------------------------------------- */

/* Private Types ---------------------------------------------------------- */

/* Private Constants ------------------------------------------------------ */

/* Private Variables (static) --------------------------------------------- ----*/

/* Global Variables ------------------------------------------------------- */

/* Private Function prototypes -------------------------------------------- --*/

/* Functions -------------------------------------------------------------*/

/**
    \fn JZ_Error_t USM_CS_Unicode2Big5(JZ_CHAR *pszDest, const JZ_WCHAR *pwszSrc, JZ_INT32 nLength)

    \brief Converts a Unicode string into a Big5 string.

    \param[out] pszDest The destination Big5 string.
    \param[in] pwszSrc The source wide-character string.
    \param[in] nLength The length of the source wide-character string. The value of 0 represents the source wide-character string ends with a null character.

    \retval JZ_SUCCESS Successful.
    \retval JZ_FAILURE Failed.
    \retval JZ_ERROR_BAD_PARAMETER Bad parameter passed.

    \remark

    \see USM_CS_Big52Unicode
*/
JZ_Error_t USM_CS_Unicode2Big5(JZ_CHAR *pszDest, const JZ_WCHAR *pwszSrc, JZ_INT32 nLength)
{
	JZ_UINT32 	nRet;
	JZ_UINT16 	nIndex = 0;
	JZ_UINT16 	nTemp;


	while (*pwszSrc)
	{
		if (*pwszSrc& 0xff80)
		{
			nRet = USM_CS_GetMatchedCode(unicode_to_big5_table, BIG5_SIZE, *pwszSrc, &nTemp);
			if (nRet == JZ_SUCCESS)
			{
				pszDest[nIndex] = (nTemp >> 8) & 0xff;
				pszDest[nIndex+1] = nTemp & 0xff;
				nIndex += 2;
			}
			else
			{
				pszDest[nIndex] = (JZ_UCHAR)nTemp;
				nIndex += 1;
			}
		}
		else
		{
			pszDest[nIndex] = (JZ_UCHAR) (*pwszSrc);
			nIndex += 1;
		}

		pwszSrc++;
	}

	pszDest[nIndex] = 0x00;

	return(JZ_SUCCESS);
}

